package com.yanqu.road.entity.activity.ruishou;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouActionConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouActionTenConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouAreaConfig;
import com.yanqu.road.entity.activity.ruishou.config.RuiShouMonsterConfig;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 瑞兽
 */
public class RuiShouConfig {

    private ActivityInfo activityInfo;

    private Map<Integer, RuiShouAreaConfig> areaConfigMap = new HashMap<>();

    /**
     * 弹珠回收奖励  区域，位置，奖励
     */
    private Map<Integer, Map<Integer, Property>> returnRewardMap = new HashMap<>();

    private Map<Integer, RuiShouMonsterConfig> monsterConfigMap = new HashMap<>();

    private Map<Integer, RuiShouActionConfig> actionConfigMap = new HashMap<>();

    private Map<Integer, RuiShouActionTenConfig> actionTenConfigMap = new HashMap<>();

    /**
     * 表现权重
     */
    private List<NormalWeightItem> actionTenWeightList = new ArrayList<>();

    /**
     * 自选奖励
     */
    private Map<Integer, Map<Integer, RuiShouMonsterAwardHelper>> awardRewardMap = new HashMap<>();

    /**
     * 瑞兽币id
     */
    private int ruiShouScoreItemId;

    /**
     * 祈福券id
     */
    private int ruiShouCostItemId;

    /**
     * 十连抽开启条件(N只瑞兽满意度为满后)
     */
    private int tenExtractOpen;

    /**
     * 十连抽消耗祈福券倍数
     */
    private int tenExtractConsume;

    /**
     * 所有瑞兽满意度上限
     */
    private int monsterHp;

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public Map<Integer, RuiShouAreaConfig> getAreaConfigMap() {
        return areaConfigMap;
    }

    public RuiShouAreaConfig getRuiShouAreaConfig(int areaId) {
        return areaConfigMap.get(areaId);
    }

    public void setAreaConfigMap(Map<Integer, RuiShouAreaConfig> areaConfigMap) {
        this.areaConfigMap = areaConfigMap;

        // 解析出来弹珠奖励
        Map<Integer, Map<Integer, Property>> tmpReturnRewardMap = new HashMap<>();
        for (RuiShouAreaConfig areaConfig : this.areaConfigMap.values()) {
            Map<Integer, Property> rewardMap = new HashMap<>();
            List<String> scores = StringUtils.stringToStringList(areaConfig.getScore(), "\\|");
            for (int i = 0; i < scores.size(); i++) {
                int pos = i + 1;
                rewardMap.put(pos, PropertyHelper.parseStringToProperty(scores.get(i)));
            }
            tmpReturnRewardMap.put(areaConfig.getArea(), rewardMap);
        }
        returnRewardMap = tmpReturnRewardMap;
    }

    /**
     * 获取回收栏奖励
     *
     * @param areaId    地区id
     * @param returnPos 回收栏位置
     */
    public Property getReturnReward(int areaId, int returnPos) {
        if (!returnRewardMap.containsKey(areaId)) {
            return null;
        }
        return returnRewardMap.get(areaId).get(returnPos);
    }

    public Map<Integer, RuiShouMonsterConfig> getMonsterConfigMap() {
        return monsterConfigMap;
    }

    public RuiShouMonsterConfig getRuiShouMonsterConfig(int monsterId) {
        return monsterConfigMap.get(monsterId);
    }

    public void setMonsterConfigMap(Map<Integer, RuiShouMonsterConfig> monsterConfigMap) {
        this.monsterConfigMap = monsterConfigMap;

        // 更新下自选奖励
        Map<Integer, Map<Integer, RuiShouMonsterAwardHelper>> tmpAwardRewardMap = new HashMap<>();
        for (RuiShouMonsterConfig monsterConfig : monsterConfigMap.values()) {
            Map<Integer, RuiShouMonsterAwardHelper> tmpMap = new HashMap<>();
            if ("0".equals(monsterConfig.getAward())) {
                continue;
            }

            // 304019=100,18|304020=100,18|304021=100,18|110260=20,10
            List<String> awardList = StringUtils.stringToStringList(monsterConfig.getAward(), "\\|");
            for (int i = 0; i < awardList.size(); i++) {
                String award = awardList.get(i);
                String[] split = award.split(","); // 304019=100,18
                RuiShouMonsterAwardHelper awardHelper = new RuiShouMonsterAwardHelper();
                awardHelper.setRewardId(i + 1);
                awardHelper.setReward(PropertyHelper.parseStringToProperty(split[0])); // 304019=100
                awardHelper.setLimit(Integer.parseInt(split[1])); // 18
                tmpMap.put(i + 1, awardHelper);
            }
            tmpAwardRewardMap.put(monsterConfig.getId(), tmpMap);
        }
        awardRewardMap = tmpAwardRewardMap;
    }

    public Map<Integer, RuiShouActionConfig> getActionConfigMap() {
        return actionConfigMap;
    }

    public RuiShouActionConfig getRuiShouActionConfig(int actionId) {
        return actionConfigMap.get(actionId);
    }

    public void setActionConfigMap(Map<Integer, RuiShouActionConfig> actionConfigMap) {
        this.actionConfigMap = actionConfigMap;
    }

    public Map<Integer, RuiShouActionTenConfig> getActionTenConfigMap() {
        return actionTenConfigMap;
    }

    public RuiShouActionTenConfig getRuiShouActionTenConfig(int id) {
        return actionTenConfigMap.get(id);
    }

    public void setActionTenConfigMap(Map<Integer, RuiShouActionTenConfig> actionTenConfigMap) {
        this.actionTenConfigMap = actionTenConfigMap;

        // 维护一个权重列表
        List<NormalWeightItem> tmpActionWeightList = new ArrayList<>();
        for (RuiShouActionTenConfig actionTenConfig : actionTenConfigMap.values()) {
            tmpActionWeightList.add(new NormalWeightItem(BigInteger.valueOf(actionTenConfig.getId()), actionTenConfig.getWeight()));
        }
        actionTenWeightList = tmpActionWeightList;
    }

    public List<NormalWeightItem> getActionTenWeightList() {
        return actionTenWeightList;
    }

    public int getActivityId() {
        return activityInfo.getActivityId();
    }

    public Map<Integer, Map<Integer, RuiShouMonsterAwardHelper>> getAwardRewardMap() {
        return awardRewardMap;
    }

    /**
     * 获取某地区等级是祥瑞的瑞兽
     *
     * @param areaId 地区id
     */
    public RuiShouMonsterConfig getMaxLevelMonsterConfig(int areaId) {
        for (RuiShouMonsterConfig monsterConfig : monsterConfigMap.values()) {
            if (monsterConfig.getArea() != areaId) {
                continue;
            }
            if (monsterConfig.getLevel() != 3) {
                continue;
            }
            return monsterConfig;
        }
        return null;
    }

    public int getTenExtractOpen() {
        return tenExtractOpen;
    }

    public void setTenExtractOpen(int tenExtractOpen) {
        this.tenExtractOpen = tenExtractOpen;
    }

    public int getTenExtractConsume() {
        return tenExtractConsume;
    }

    public void setTenExtractConsume(int tenExtractConsume) {
        this.tenExtractConsume = tenExtractConsume;
    }

    public int getMonsterHp() {
        return monsterHp;
    }

    public void setMonsterHp(int monsterHp) {
        this.monsterHp = monsterHp;
    }

    public int getRuiShouScoreItemId() {
        return ruiShouScoreItemId;
    }

    public void setRuiShouScoreItemId(int ruiShouScoreItemId) {
        this.ruiShouScoreItemId = ruiShouScoreItemId;
    }

    public int getRuiShouCostItemId() {
        return ruiShouCostItemId;
    }

    public void setRuiShouCostItemId(int ruiShouCostItemId) {
        this.ruiShouCostItemId = ruiShouCostItemId;
    }
}
